package com.jia.lidao.ui.order;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jia.lidao.Constans;
import com.jia.lidao.R;
import com.jia.lidao.UserUtils;
import com.jia.lidao.enity.Loan;
import com.jia.lidao.enity.OrderLoanRespDTO;
import com.jia.lidao.enity.RefreshStatusMessage;
import com.jia.lidao.enity.Remittance;
import com.jia.lidao.enity.RemittanceRespDTO;
import com.jia.lidao.network.http.BusinessManager;
import com.jia.lidao.network.http.MyCallback;
import com.jia.lidao.network.http.request.impl.DealAuditOrderRequest;
import com.jia.lidao.network.http.request.impl.DealWithLoanRequest;
import com.jia.lidao.network.http.request.impl.GetOrderAuditRequest;
import com.jia.lidao.network.http.request.impl.LoanDetailRequest;
import com.jia.lidao.ui.BaseActivity;
import com.jia.lidao.ui.ImageActivity;
import com.jia.lidao.utils.LogUtil;
import com.jia.lidao.widget.CommonTitleBar;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by jia on 17/12/23.
 */

public class AuditActivity extends BaseActivity {


    private int payment_type;

    private RelativeLayout auditOrderrl;

    private LinearLayout auditotherLl;

    private CommonTitleBar titleBar;

    private int id, payStatus, role;

    public static AuditActivity instance;

    private TextView otherSn, otherName, otherTime, otherAmount, typeTxt;

    private TextView ordersn, orderName, ordercount;

    private TextView passTxt, feedbackTxt;

    private ImageView img;


    private LinearLayout dealll;

    private Remittance remittance;

    private Loan loan;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actvity_audit);
        instance = this;
        payment_type = getIntent().getIntExtra("paymenttype", 5);
        role = getIntent().getIntExtra("role", 0);
        payStatus = getIntent().getIntExtra("status", 0);
        LogUtil.printJ("type=" + payment_type);
        id = getIntent().getIntExtra("ID", 0);
        if (TextUtils.isEmpty(getIntent().getStringExtra("remittance"))) {
            remittance = null;
        } else {
            remittance = new Gson().fromJson(getIntent().getStringExtra("remittance"), Remittance.class);
        }

        if (TextUtils.isEmpty(getIntent().getStringExtra("loan"))) {
            loan = null;
        } else {
            loan = new Gson().fromJson(getIntent().getStringExtra("loan"), Loan.class);
        }
        initView();
    }

    private void initView() {
        titleBar = (CommonTitleBar) findViewById(R.id.common_title_bar);
        auditOrderrl = (RelativeLayout) findViewById(R.id.order_audit_rl);
        auditotherLl = (LinearLayout) findViewById(R.id.other_audit_ll);
        otherAmount = (TextView) findViewById(R.id.tv_other_price);
        otherName = (TextView) findViewById(R.id.tv_other_name);
        otherSn = (TextView) findViewById(R.id.tv_other_sn);
        otherTime = (TextView) findViewById(R.id.tv_other_time);
        orderName = (TextView) findViewById(R.id.tv_name);
        ordersn = (TextView) findViewById(R.id.tv_order_sn);
        ordercount = (TextView) findViewById(R.id.tv_order_count);
        img = (ImageView) findViewById(R.id.order_img);
        passTxt = (TextView) findViewById(R.id.tv_audi_pass);
        feedbackTxt = (TextView) findViewById(R.id.tv_audi_feedback);
        typeTxt = (TextView) findViewById(R.id.tv_typename);
        dealll = (LinearLayout) findViewById(R.id.deal_ll);
        if (UserUtils.getInstance().getAgent() != null) {
            if (UserUtils.getInstance().getAgent().type == 2) {
                typeTxt.setText("农户:    ");
            }
        }
        Log.e("1234","status="+payStatus+" role="+role);
        if (payStatus != 2 || role == Constans.ORDERRECEIVE) {
            dealll.setVisibility(View.GONE);
        } else {
            dealll.setVisibility(View.VISIBLE);
        }

        setData();
    }


    private void setData() {
        if (payment_type == 5) {
            titleBar.setTitle("订货审核");
            auditOrderrl.setVisibility(View.VISIBLE);
            auditotherLl.setVisibility(View.GONE);
            if (remittance == null) {
                BusinessManager.getInstance().getOrderAuditInfo(new GetOrderAuditRequest(id), new MyCallback<RemittanceRespDTO>() {
                    @Override
                    public void onSuccess(final RemittanceRespDTO result, String message) {
                        if (result == null || result.remittance == null) {
                            showToast("获取信息错误");
                            return;
                        }
                        ordercount.setText(result.remittance.order.goods_count + "");
                        if (!TextUtils.isEmpty(result.remittance.agent.nickname)) {
                            orderName.setText(result.remittance.agent.nickname);
                        } else {
                            orderName.setText(result.remittance.agent.realname);
                        }
                        img.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startActivity(new Intent(AuditActivity.this, ImageActivity.class).putExtra("url", result.remittance.pic));
                            }
                        });
                        ordersn.setText(result.remittance.order.order_sn);
                        ImageLoader.getInstance().displayImage(result.remittance.pic, img);
                        passTxt.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                showDialog("");
                                BusinessManager.getInstance().dealWithOrderAudit(new DealAuditOrderRequest(result.remittance.remittance_id, 2, ""), new MyCallback<Void>() {
                                    @Override
                                    public void onSuccess(Void result, String message) {
                                        dismissDialog();
                                        EventBus.getDefault().post(new RefreshStatusMessage());
                                        showToast("审核通过");
                                        finish();
                                    }

                                    @Override
                                    public void onError(String errorCode, String message) {
                                        dismissDialog();
                                        showToast(message);
                                    }

                                    @Override
                                    public void onFinished() {

                                    }

                                    @Override
                                    public void onRefreshToken() {
                                        UserUtils.getInstance().goToLogin(AuditActivity.this);
                                    }
                                });
                            }
                        });
                        feedbackTxt.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                startActivity(new Intent(AuditActivity.this, FeedBackActivity.class).putExtra("ID", result.remittance.remittance_id).putExtra("pay", 5));
                            }
                        });


                    }

                    @Override
                    public void onError(String errorCode, String message) {

                    }

                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public void onRefreshToken() {
                        UserUtils.getInstance().goToLogin(AuditActivity.this);
                    }
                });
            } else {
                ordercount.setText(remittance.order.goods_count + "");
                if (!TextUtils.isEmpty(remittance.agent.nickname)) {
                    orderName.setText(remittance.agent.nickname);
                } else {
                    orderName.setText(remittance.agent.realname);
                }
                img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(AuditActivity.this, ImageActivity.class).putExtra("url", remittance.pic));
                    }
                });
                ordersn.setText(remittance.order.order_sn);
                ImageLoader.getInstance().displayImage(remittance.pic, img);
                passTxt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        showDialog("");
                        BusinessManager.getInstance().dealWithOrderAudit(new DealAuditOrderRequest(remittance.remittance_id, 2, ""), new MyCallback<Void>() {
                            @Override
                            public void onSuccess(Void result, String message) {
                                dismissDialog();
                                EventBus.getDefault().post(new RefreshStatusMessage());
                                showToast("审核通过");
                                finish();
                            }

                            @Override
                            public void onError(String errorCode, String message) {
                                dismissDialog();
                                showToast(message);
                            }

                            @Override
                            public void onFinished() {

                            }

                            @Override
                            public void onRefreshToken() {
                                UserUtils.getInstance().goToLogin(AuditActivity.this);
                            }
                        });
                    }
                });
                feedbackTxt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(AuditActivity.this, FeedBackActivity.class).putExtra("ID", remittance.remittance_id).putExtra("pay", 5));
                    }
                });
            }
        } else {
            titleBar.setTitle("农资贷审核");
            auditOrderrl.setVisibility(View.GONE);
            auditotherLl.setVisibility(View.VISIBLE);
            if (loan == null) {
                BusinessManager.getInstance().getLoanDetailInfo(new LoanDetailRequest(id), new MyCallback<OrderLoanRespDTO>() {
                    @Override
                    public void onSuccess(final OrderLoanRespDTO result, String message) {
                        if (result == null || result.loan == null) {
                            showToast("获取信息错误");
                            return;
                        }
                        otherAmount.setText("¥" + result.loan.amount);
                        if (!TextUtils.isEmpty(result.loan.borrower.nickname)) {
                            otherName.setText(result.loan.borrower.nickname);
                        } else {
                            otherName.setText(result.loan.borrower.realname);
                        }
                        otherSn.setText(result.loan.order.order_sn);
                        otherTime.setText(result.loan.start_date + "至" + result.loan.end_date);
                        passTxt.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                showDialog("");
                                BusinessManager.getInstance().dealWithLoanAudit(new DealWithLoanRequest(result.loan.id, 2, ""), new MyCallback<Void>() {
                                    @Override
                                    public void onSuccess(Void result, String message) {
                                        dismissDialog();
                                        EventBus.getDefault().post(new RefreshStatusMessage());
                                        showToast(message);
                                        finish();
                                    }

                                    @Override
                                    public void onError(String errorCode, String message) {
                                        dismissDialog();
                                        showToast(message);
                                    }

                                    @Override
                                    public void onFinished() {

                                    }

                                    @Override
                                    public void onRefreshToken() {
                                        UserUtils.getInstance().goToLogin(AuditActivity.this);
                                    }
                                });
                            }
                        });

                        feedbackTxt.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                startActivity(new Intent(AuditActivity.this, FeedBackActivity.class).putExtra("ID", result.loan.id).putExtra("pay", 6));
                            }
                        });


                    }

                    @Override
                    public void onError(String errorCode, String message) {

                    }

                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public void onRefreshToken() {
                        UserUtils.getInstance().goToLogin(AuditActivity.this);
                    }
                });
            } else {
                otherAmount.setText("¥" + loan.amount);
                if (!TextUtils.isEmpty(loan.borrower.nickname)) {
                    otherName.setText(loan.borrower.nickname);
                } else {
                    otherName.setText(loan.borrower.realname);
                }
                otherSn.setText(loan.order.order_sn);
                otherTime.setText(loan.start_date + "至" + loan.end_date);
                passTxt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        showDialog("");
                        BusinessManager.getInstance().dealWithLoanAudit(new DealWithLoanRequest(loan.id, 2, ""), new MyCallback<Void>() {
                            @Override
                            public void onSuccess(Void result, String message) {
                                dismissDialog();
                                EventBus.getDefault().post(new RefreshStatusMessage());
                                showToast(message);
                                finish();
                            }

                            @Override
                            public void onError(String errorCode, String message) {
                                dismissDialog();
                                showToast(message);
                            }

                            @Override
                            public void onFinished() {

                            }

                            @Override
                            public void onRefreshToken() {
                                UserUtils.getInstance().goToLogin(AuditActivity.this);
                            }
                        });
                    }
                });

                feedbackTxt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(AuditActivity.this, FeedBackActivity.class).putExtra("ID", loan.id).putExtra("pay", 6));
                    }
                });
            }
        }
    }

}
